//
//  RegisterProtocolView.m
//  ShowYork
//
//  Created by hxh on 2020/8/24.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "RegisterProtocolView.h"

typedef void(^doneBlock)(BOOL status);
typedef void(^protocolUrlBlock)(NSDictionary *param);

@interface RegisterProtocolView ()<TTTAttributedLabelDelegate>
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)TTTAttributedLabel *contentLb;

@property (nonatomic,copy)doneBlock done;

@property (nonatomic,copy)protocolUrlBlock protocolUrl;

@end

@implementation RegisterProtocolView

- (instancetype)initWithDoneBlock:(void (^)(BOOL stauts))doneCompleteBlock protocolBlock:(void (^)(NSDictionary * _Nonnull urlParam))protocolCompleteBlock{
    if (self = [super init]) {
        [self setUI];
        if (doneCompleteBlock) {
            self.done = ^(BOOL status) {
                doneCompleteBlock(status);
            };
        }
        if (protocolCompleteBlock) {
            self.protocolUrl = ^(NSDictionary *param) {
                protocolCompleteBlock(param);
            };
        }
    }
    return self;
}

- (void)setUI{
    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setImage:[UIImage imageNamed:@"common_select_tag"] forState:UIControlStateNormal];
    [self.confirmBtn setImage:[UIImage imageNamed:@"common_selected_tag"] forState:UIControlStateSelected];
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.confirmBtn];
    
    self.contentLb = [[TTTAttributedLabel alloc]initWithFrame:CGRectZero];
    self.contentLb.numberOfLines = 0;
    self.contentLb.delegate = self;
    self.contentLb.font = [UIFont systemFontOfSize:14];
    self.contentLb.linkAttributes = @{(NSString *)kCTForegroundColorAttributeName:[RPAppManager themColor],
                                  (NSString *)kCTUnderlineStyleAttributeName:[NSNumber numberWithBool:NO]};
    [self addSubview:self.contentLb];
    
    self.contentLb.text = @"登录即表明同意《服务协议》和《用户隐私政策》";

    NSRange selRange1 = [self.contentLb.text rangeOfString:@"《服务协议》"];
    NSRange selRange2 = [self.contentLb.text rangeOfString:@"《用户隐私政策》"];
    [self.contentLb addLinkToTransitInformation:@{@"data":[NSString stringWithFormat:@"%@user/agreement?type=serviceAgreement&navbar=no",HtmlUrl],@"type":@1} withRange:selRange1];
    [self.contentLb addLinkToTransitInformation:@{@"data":[NSString stringWithFormat:@"%@user/agreement?type=userPrivacy&navbar=no",HtmlUrl],@"type":@2} withRange:selRange2];
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self);
        make.left.equalTo(self.confirmBtn.mas_right).offset(5);
    }];

}


- (void)confirmMethod{
    self.confirmBtn.selected = !self.confirmBtn.selected;
    if (self.done) {
        self.done(self.confirmBtn.selected);
    }
}
#pragma mark - 超链接的点击代理方法
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTransitInformation:(NSDictionary *)components {
    if (self.protocolUrl) {
        self.protocolUrl(components);
    }
}


@end
